package edu.example;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;


public class viewReviews extends JDialog
{
	private Application app;
	private viewAppFrame backScreen;
	private int reviewNum;
	private Vector<Review> reviews = new Vector<Review>();
	private ReviewManager rm;
	private UserManager um;

	private	JTabbedPane tabbedPane;
	
	private JButton okButton;
	
	private JPanel buttonPanel;

	private OkHandler okHandler;
	
	public viewReviews(Application a, viewAppFrame initBack, ReviewManager initRM, UserManager initUM)
	{
		um = initUM;
		app = a;
		rm = initRM;
		reviews = rm.getReviewsByApp(a);
		reviewNum = reviews.size();
		backScreen = initBack;
		//reviews=rm.getallReviews();
		this.setSize(1280, 720);
		layoutGUI();
	}

	private void layoutGUI() 
	{
		// size and locate the dialog at the center of the screen
		int w = getWidth();
		int h = getHeight();
		Toolkit tk = Toolkit.getDefaultToolkit();
		Dimension screenSize = tk.getScreenSize();
		int screenWidth = screenSize.width;
		int screenHeight = screenSize.height;
		int x = (screenWidth - w)/2;
		int y = (screenHeight - h)/2;
		setLocation(x,y);
		
		//JPanel panel = new JPanel();
		tabbedPane = new JTabbedPane();

		okButton = new JButton("OK");
		buttonPanel = new JPanel();
	
		for(int i = 0; i < reviewNum; i++)
		{
			JLabel nameLabel = new JLabel("Name: "); 
			JTextField nameField = new JTextField(10);
			nameField.setText(reviews.get(i).getReviewer().getName());
			nameField.setEditable(false);
			
			JTextArea reviewArea = new JTextArea(30, 110);
			reviewArea.setText(reviews.get(i).getcontent());
			reviewArea.setEditable(false);
			
			JPanel panel = new JPanel();
			GridBagLayout layout = new GridBagLayout();
			panel.setLayout(layout);
			
			addComponent(nameLabel,		panel, 0, i, 	1, 1);
			addComponent(nameField,		panel, 1, i, 	1, 1);
			addComponent(reviewArea,	panel, 0, i+1, 	2, 1);
			
			tabbedPane.add(panel);
			tabbedPane.setTitleAt(i, reviews.get(i).getReviewer().getName());
		}
		buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
		buttonPanel.add(okButton);
		
		this.add(tabbedPane, BorderLayout.CENTER);
		this.add(buttonPanel, BorderLayout.SOUTH);
		
		okHandler = new OkHandler();
		okButton.addActionListener(okHandler);
	}
	
	/**
	 * GridBadLayout helper method
	 */
	public void addComponent(	JComponent jc,
			Container c,
			int x,
			int y,
			int w,
			int h)
	{
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = x;
		gbc.gridy = y;
		gbc.gridwidth = w;
		gbc.gridheight = h;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.insets = new Insets(5, 5, 5, 5);
		c.add(jc, gbc);
	}
	/*
	public static void main(String[] args)
	{
		File file1 = new File("AY.txt");
		File file2 = new File("AY-map.txt");
		File file3 = new File("OTS.txt");
		File file4 = new File("OTS-map.txt");
		ApplicationManagerCloud formatter = new ApplicationManagerCloud(null, file1, file2, file3, file4);
		UserManager um = new UserManager();
		ReviewManager rm = new ReviewManager(um);
		Application tempApp = formatter.getAllApps().get("alan_perlis@amail.com");
		tempApp.setNumDesiredReviews(10);
		Reviewer Rr1 = new Reviewer("abc@mail.com","name","132",true);
		Review r1 = new Review(Rr1, tempApp);
		Review r2 = new Review(Rr1, tempApp);
		rm.addReview(r1);
		rm.addReview(r2);
		viewReviews temp = new viewReviews(tempApp, null, rm);
		temp.setVisible(true);
	}	
	*/
	
	class OkHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent arg0) 
		{
			viewReviews.this.setVisible(false);
		}
	}
}
